package com.ljkj.blockpay.basedata;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.ljkj.blockpay.R;
import com.ljkj.blockpay.widget.changestatusBarcolor.Eyes;
import com.trello.rxlifecycle.components.support.RxFragmentActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * Created by tianping on 2020/3/16.
 */

public abstract class BaseFragmentActivity2<V extends ViewDataBinding, P extends BasePresenterImpl> extends RxFragmentActivity {
    protected V binding;
    private P mPresenter;//功能调用
    protected Activity baseContext;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.baseContext = BaseFragmentActivity2.this;
        initViewDataBinding();
        setPresenter();
        init(savedInstanceState);
        initData();
        Eyes.setStatusBarColor2(this, getResources().getColor(R.color.color000000_100), getResources().getColor(R.color.color000000_100));

        compatible();
    }

    /**
     * 注入绑定
     */
    private void initViewDataBinding() {
        //DataBindingUtil类需要在project的build中配置 dataBinding {enabled true }, 同步后会自动关联android.databinding包
        binding = DataBindingUtil.setContentView(this, initContentView());
    }

    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView();

    /**
     * 初始化网络请求presenter
     *
     * @return 布局layout的id
     */
    protected abstract P createPresenter();

    protected abstract void init(Bundle savedInstanceState);

    /**
     * 获取presenter
     *
     * @return
     */
    protected P getPresenter() {
        return mPresenter;
    }

    protected void setPresenter() {
        if (mPresenter == null)
            mPresenter = createPresenter();
    }

    protected void initData() {

    }

    // 判断权限集合
    protected boolean needPermissions(String... permissions) {
        //判断版本是否兼容
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return false;
        }
        boolean isNeed;
        for (String permission : permissions) {
            isNeed = needsPermission(permission);
            if (isNeed) {
                return true;
            }
        }
        return false;
    }

    protected List<String> noPermissions(String... permissions) {
        List<String> list = new ArrayList<>();
        for (String permission : permissions) {
            if (needPermissions(permission)) {
                list.add(permission);
            }
        }
        return list;
    }

    // 判断是否缺少权限
    protected boolean needsPermission(String permission) {
        return ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 注册全局监听事件
     */
    protected void registerEventListener() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    /**
     * 注销全局监听事件
     */
    protected void removeEventListener() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
            EventBus.getDefault().removeAllStickyEvents();
        }
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        removeEventListener();
        super.onDestroy();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void compatible() {
        //兼容8.1系统输入框获取焦点问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            getWindow().getDecorView().setImportantForAutofill(
                    View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
        }
    }
}
